package com.focusai.efairy.ui.activity.warn;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.msg.MsgItem;
import com.focusai.efairy.model.request.GetWarnDetailInfoRequest;
import com.focusai.efairy.model.warn.AlarmItem;
import com.focusai.efairy.model.warn.DevWarnDetailInfo;
import com.focusai.efairy.network.HttpManager;
import com.focusai.efairy.network.Image.ImageLoaderFactory;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;
import com.focusai.efairy.ui.widget.ninegridview.NineGridTestLayout;
import com.focusai.efairy.utils.ActivityUtils;
import com.focusai.efairy.utils.StrByCodeUtils;

import java.util.ArrayList;

/**
 * Created by luo_guangwen on 2017/10/11.
 */

public class WarnningDetailActivity extends SwipeBackBaseActivity {
    public static final String KEY_MSG_ITEM = "key_msg_item";
    public static final String KEY_ALARM_ITEM = "key_alarm_item";
    private ImageView ivAvater;
    private TextView textHeadName;
    private TextView textHeadContent;
    private TextView textHeadTip;
    private TextView textContent;
    private NineGridTestLayout nineGridTestLayout;
    private TextView textTimes;
    private MsgItem msgItem;
    private AlarmItem alarmItem; //详情时传过来

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_warn_detail);
        initView();
        setDefaultValues();
    }

    @Override
    protected void initView() {
        textHeadName = findView(R.id.text_heard_name);
        textHeadContent = findView(R.id.text_heard_content);
        textHeadTip = findView(R.id.text_heard_tip);
        textTimes = findView(R.id.text_food_time);
        textContent = findView(R.id.text_content);
        ivAvater = findView(R.id.iv_user_avater);
        nineGridTestLayout = findView(R.id.layout_nine_grid);
    }

    @Override
    protected void setDefaultValues() {
        setCenterTitleName("报警处理详情");
        if (getIntent() != null) {
            msgItem = (MsgItem) getIntent().getSerializableExtra(KEY_MSG_ITEM);
            alarmItem = (AlarmItem) getIntent().getSerializableExtra(KEY_ALARM_ITEM);
        }
        if (alarmItem != null) {
            showDetail(alarmItem);
            return;
        }
        if (msgItem == null) {
            showToast("传递的消息为空");
            finish();
        }
        findView(R.id.ll_content).setVisibility(View.GONE);
        loadData();
    }

    @Override
    protected void loadData() {
        showSingleDialog("正在加载数据..");
        HttpManager.getInstance().sendRequest(new GetWarnDetailInfoRequest(msgItem.get_id(), new Response.Listener<DevWarnDetailInfo>() {
            @Override
            protected void onSuccess(DevWarnDetailInfo response) {
                closeSingleDialog();
                if (response != null) {
                    findView(R.id.ll_content).setVisibility(View.VISIBLE);
                    textHeadName.setText(response.efairyuser_nickname);
                    textHeadContent.setText(response.efairydevice_name);
                    textContent.setText(response.efairyalarmrecord_content);
                    textTimes.setText(response.efairyalarmrecord_add_time);
                    String tip = StrByCodeUtils.getStatusByAlarmId(response.efairyalarmrecord_alarm_id) + "  ";
                    if (response.efairyalarmrecord_is_handle == 1) {
                        tip += "隐患已排除";
                    } else {
                        tip += "隐患未排除";
                    }
                    textHeadTip.setText(tip);
                    ImageLoaderFactory.getImageLoader().loadImage(mContext, ivAvater, response.efairyuser_headimg_url, R.drawable.user_default_icon, false);
                    nineGridTestLayout.setUrlList(response.efairyalarmrecord_imgurl_list);
                    nineGridTestLayout.setOnImageItemClick(new NineGridTestLayout.OnImageItemClick() {
                        @Override
                        public void onImageItemClick(int position, String url, ArrayList<String> urlList) {
                            ActivityUtils.openPhotoPagerActivity(WarnningDetailActivity.this, 0, position, urlList, false);
                        }
                    });
                }
            }

            @Override
            protected void onFail(NetworkException exception) {
                closeSingleDialog();
            }
        }));
    }

    private void showDetail(AlarmItem alarmItem) {
        findView(R.id.ll_content).setVisibility(View.VISIBLE);
        textHeadName.setText(alarmItem.efairyuser_nickname);
        textHeadContent.setText(alarmItem.efairydevice_name);
        textContent.setText(alarmItem.efairyalarmrecord_content);
        textTimes.setText(alarmItem.efairyalarmrecord_add_time);
        String tip = StrByCodeUtils.getStatusByAlarmId(alarmItem.efairyalarmrecord_alarm_id) + "  ";
        if (alarmItem.efairyalarmrecord_is_handle == 1) {
            tip += "隐患已排除";
        } else {
            tip += "隐患未排除";
        }
        textHeadTip.setText(tip);
        ImageLoaderFactory.getImageLoader().loadImage(mContext, ivAvater, alarmItem.efairyuser_headimg_url, R.drawable.user_default_icon, false);
        nineGridTestLayout.setUrlList(alarmItem.efairyalarmrecord_imgurl_list);
        nineGridTestLayout.setOnImageItemClick(new NineGridTestLayout.OnImageItemClick() {
            @Override
            public void onImageItemClick(int position, String url, ArrayList<String> urlList) {
                ActivityUtils.openPhotoPagerActivity(WarnningDetailActivity.this, 0, position, urlList, false);
            }
        });
    }
}
